package com.simple.controller;

import com.simple.common.enumdto.BillType;
import com.simple.common.Result;
import com.simple.dto.BillDTO;
import com.simple.dto.BillTypeDTO;
import com.simple.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName BillController
 * @Author SimpleTree
 **/
@RestController
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @GetMapping
    public Result getBills(BillDTO billDTO) {
        return Result.of(billService.getBills(billDTO));
    }

    @GetMapping("/getBillById/{id}")
    public Result getBillById(@PathVariable("id")Integer id) {
        return Result.of(billService.getBillById(id));
    }

    @GetMapping("/getBillTypes")
    public Result getBillTypes() {
        return Result.of(Arrays.stream(BillType.values()).map(value -> new BillTypeDTO(value.getCode(), value.getMsg())).collect(Collectors.toList()));
    }

    @PostMapping
    public Result saveBill(@RequestBody BillDTO billDTO) {
        billService.saveBill(billDTO);
        return Result.of();
    }

    @PutMapping
    public Result updateBill(@RequestBody BillDTO billDTO) {
        billService.updateBill(billDTO);
        return Result.of();
    }

    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Integer> ids) {
        billService.deleteByIds(ids);
        return Result.of();
    }

    @GetMapping("/getMonthBillMoney/{userId}")
    public Result getMonthBillMoney(@PathVariable("userId")Integer userId) {
        return Result.of(billService.getMonthBillMoney(userId));
    }

    @GetMapping("/getMoneyOfMonthByBillType")
    public Result getMoneyOfMonthByBillType(@RequestParam Integer billTypeCode,@RequestParam Integer userId) {
        return Result.of(billService.getMoneyOfMonthByBillType(billTypeCode,userId));
    }
}
